// approval/pages/items_approval/items_approval.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgApproval: '',
    readonly: true,
    stautsApproval: true,
    readDataShow: false,
    examMain: '',
    listProps: {
      topList: [],
      btmList: [],
      readonlyStatus: false,
    },
    examInfo: {
      examMemberDetail: '',
      showBtn: false,
    },
    materialExam: '',
    isMusk: false, //遮罩
    goodsList: [],
    productList: {
      list: [],
      list1: []
    },
    currentDate: new Date(), //选择时间
    showTime: false, //选择时间弹窗
    value: '',
    formatter(type, val) {
      if (type === 'year') {
        return val + '年';
      }
      if (type === 'month') {
        return val + '月';
      }
      if (type === 'day') {
        return val + '日';
      }
      return val;
    },
  },


  // 添加物品明细
  addData() {
    if (this.data.listProps.readonlyStatus) {
      return false
    }
    var goodsInfo = {
      name: '',
      num: '',
      status: '-1',
      allNum: '',
      id: ''
    }
    var goodsList = this.data.goodsList;
    goodsList.push(goodsInfo);
    this.setData({
      goodsList
    })
  },

  //删除物品
  delGoods(e) {
    var index = e.currentTarget.dataset.index,
      goodsList = this.data.goodsList,
      that = this;
    if (this.data.listProps.readonlyStatus) {
      return false
    }
    wx.showModal({
      title: '物料领用',
      content: '您将要删除此项',
      complete: (res) => {
        if (res.confirm) {
          goodsList.splice(index, 1)
          that.setData({
            goodsList
          })
        }
      }
    })
  },

  // 输入框绑定
  inputHandle(e) {
    console.log(e);
    var val = e.detail,
      str = e.currentTarget.dataset.str;
    this.setData({
      [str]: val
    })
    console.log(this.data);
  },
  // 输入商品数量
  numInput(e) {
    var index = e.currentTarget.dataset.index,
      val = e.detail,
      goodsList = this.data.goodsList;
    console.log(index);
    console.log(val);
    goodsList[index].material_num = val
    console.log(goodsList[index].material_name);
    if (goodsList[index].material_name == undefined) {
      wx.showToast({
        title: '请先选择申请物料',
        icon: 'none'
      })
      return
    }
    this.setData({
      goodsList
    })
  },

  // 获取物品列表
  getMaterial() {
    wx.showLoading({
      title: ''
    })
    var url = app.globalData.url + 'material/getMaterial',
      that = this,
      columns = [];
    app.wxRequest(url, {
      materialName: '',
      materialType: -1,
    }, res => {
      if (res.flag) {
        columns = res.data.materials
        that.setData({
          columns
        })
        wx.hideLoading()
      }
    })
  },

  // 选择物料
  dialogShow(e) {
    var index = e.currentTarget.dataset.index;
    console.log(index);

    if (this.data.listProps.readonlyStatus) {
      return false
    }
    this.setData({
      listIndex: index,
      showGoods: true
    })
  },

  // 选择物料 确认
  onConfirm(e) {
    console.log(e);
    var val = e.detail.value,
      that = this,
      goodsList = this.data.goodsList,
      listIndex = this.data.listIndex;
    goodsList[listIndex].material_name = val.material_name;
    goodsList[listIndex].id = val.material_id;
    goodsList[listIndex].status = val.material_type;
    goodsList[listIndex].allNum = val.stock_num;
    that.setData({
      goodsList,
      showGoods: false
    })

  },
  // 选择物料  取消
  onCancel() {
    this.setData({
      showGoods: false
    })
  },
  // 校验物料数量
  numBlur(e) {
    var index = e.currentTarget.dataset.index,
      val = e.detail.value,
      goodsList = this.data.goodsList;
    if (goodsList[index].allNum < val) {
      wx.showToast({
        title: '注意：申请物料数量大于了总库存',
        icon: 'none'
      })
      goodsList[index].material_num = 0
      this.setData({
        goodsList
      })
    }
  },
  // 组件事件
  change(obj) {
    console.log(obj);
    this.setData({
      listProps: JSON.parse(wx.getStorageSync('listProps'))
    })
    console.log(this.data.listProps);
  },
  // 获取 审批人  抄送人
  getExamProcessDetail() {
    wx.showLoading({
      title: '',
      icon: 'none'
    })
    var url = app.globalData.url + "exam/getExamProcessDetail",
      that = this,
      listProps = this.data.listProps;
    app.wxRequest(url, {
      examType: 5,
      memberId: app.globalData.memberId
    }, res => {
      console.log(res);
      if (res.flag) {
        listProps.topList = res.data['审批人'];
        listProps.btmList = res.data['抄送人'];
        console.log(listProps);
        that.setData({
          listProps,
        })
        wx.hideLoading()
      }
    })
  },
  // 审批 类
  resultData() {
    wx.showLoading({
      title: '',
    })
    var examInfo = JSON.parse(wx.getStorageSync('examInfo')),
      listProps = this.data.listProps;
    this.setData({
      readonly: true,
      examInfo
    })
    console.log(this.data.examInfo);
    this.getExamDetail();
    if (this.data.examInfo.examMember_status == 4) {
      this.setData({
        readDataShow: true
      })
    }
    // if (wx.getStorageSync('readDataOa') != '') {
    //   this.setData({
    //     readDataShow: true
    //   })
    // }
    if (examInfo.stautsApproval != undefined) {
      this.setData({
        stautsApproval: false
      })
    }
    if (!examInfo.detial) {
      listProps.readonlyStatus = true
      this.getPenSigner()
      this.setData({
        listProps
      })
    }
    wx.removeStorageSync('examInfo')
    wx.removeStorageSync('readDataOa')
  },

  // 获取物料申请详情
  getExamDetail() {
    wx.showLoading({
      title: '',
      icon: 'none'
    })
    var url = app.globalData.url + "exam/getExamDetail",
      that = this;
    app.wxRequest(url, {
      examId: that.data.examInfo.examMain_id
    }, res => {
      if (res.flag) {
        console.log(res);
        var examMain = [],
          materialExam = [],
          productList = this.data.productList,
          listProps = this.data.listProps;
        examMain = res.data.examMain;
        var afterDate = new Date(examMain.date_time.replace(/-/g, "/"))
        examMain.date_time = afterDate.getFullYear() + '-' + that.add0((afterDate.getMonth() + 1)) + '-' + that.add0(afterDate.getDate());
        examMain.date_time = examMain.date_time;
        materialExam = res.data.materialExam;
        productList.list = [];
        productList.list1 = [];
        for (var i in res.data.materialExamDetails) {
          if (res.data.materialExamDetails[i].material_type == 0) {
            productList.list.push(res.data.materialExamDetails[i]);
          } else if (res.data.materialExamDetails[i].material_type == 1) {
            productList.list1.push(res.data.materialExamDetails[i]);
          }
        }
        for (var i in res.data.examMembers) {
          res.data.examMembers[i].memberName = res.data.examMembers[i].member_name;
          res.data.examMembers[i].memberImg = res.data.examMembers[i].member_img;
          if (res.data.examMembers[i].examMember_type == 0) {
            listProps.topList.push(res.data.examMembers[i])
          } else {
            listProps.btmList.push(res.data.examMembers[i])
          }
        }
        that.setData({
          examMain,
          productList,
          listProps,
          materialExam
        })
        wx.hideLoading()
      }
    })
  },
  add0(num) {
    return num < 10 ? '0' + num : num
  },


  // 同意  不同意
  approvalData(e) {
    var flag = e.currentTarget.dataset.flag;
    if (!this.data.examInfo.detial && this.data.imgApproval.length == 0) {
      wx.showModal({
        title: '提示',
        content: '暂无签名板,请完善签名板!',
        complete: (res) => {
          if (res.confirm) {
            console.log('签名版');
            wx.navigateTo({
              url: '/gerenInfo/pages/signature/signature',
            })
          }
        }
      })
      return
    }
    var examMemberNum = '',
      examMemberType = '',
      listProps = this.data.listProps,
      url = app.globalData.url + 'exam/reviewExam',
      that = this;
    for (let i in listProps.topList) {
      if (listProps.topList[i].member_id == wx.getStorageSync('member_id')) {
        examMemberNum = listProps.topList[i].examMember_num;
        examMemberType = listProps.topList[i].examMember_type;
      }
    }
    wx.showLoading({
      title: '',
      icon: 'none'
    })
    app.wxRequest(url, {
      examMemberId: that.data.examInfo.examMember_id,
      examMemberNum: examMemberNum,
      examMemberDetail: that.data.examInfo.examMemberDetail == undefined ? '' : that.data.examInfo.examMemberDetail,
      examMemberStatus: flag,
      examType: 5,
      note: that.data.imgApproval,
      examMainId: that.data.examInfo.examMain_id,
      examMemberType: examMemberType
    }, res => {
      if (res.flag) {
        wx.hideLoading()
        wx.showModal({
          title: '提示',
          content: '审核成功',
          showCancel: false,
          complete: (res) => {
            if (res.confirm) {
              wx.navigateBack()
            }
          }
        })
      }
    })
  },

  // 确认提交
  addMaterialCancelExam() {
    var examMemberIdList = [],
      leaveExamTypeList = [],
      listProps = this.data.listProps;
    console.log(listProps);
    for (let i in listProps.topList) {
      if (listProps.topList[i].member_id == undefined) {
        examMemberIdList.push(listProps.topList[i].memberId);
      } else {
        examMemberIdList.push(listProps.topList[i].member_id);
      }
      leaveExamTypeList.push(0)
    }
    for (let i in listProps.btmList) {
      if (listProps.btmList[i].member_id == undefined) {
        examMemberIdList.push(listProps.btmList[i].memberId);
      } else {
        examMemberIdList.push(listProps.btmList[i].member_id);
      }
      leaveExamTypeList.push(1)
    }
    var url = app.globalData.url + "exam/addMaterialCancelExam",
      that = this;
    console.log(examMemberIdList);
    console.log(leaveExamTypeList);
    app.wxRequest(url, {
      memberId: app.globalData.memberId,
      examStatus: 0,
      examType: 5,
      materialExamMainId: that.data.examMain.examMain_id,
      examMemberId: examMemberIdList.toString(),
      examLeaveExamType: leaveExamTypeList.toString(),
    }, res => {
      if (res.flag) {
        console.log(res);
        wx.hideLoading()
        wx.showModal({
          title: '提示',
          content: '提交成功',
          showCancel: false,
          complete: (res) => {
            if (res.confirm) {
              wx.navigateBack()
            }
          }
        })
      }
    })
  },

  // 撤回
  revokeExam() {
    wx.showLoading({
      title: '',
      icon: 'none'
    })
    console.log(this.data.examInfo);
    var url = app.globalData.url + 'exam/revokeExam',
      that = this;
    app.wxRequest(url, {
      examMainId: that.data.examInfo.examMain_id
    }, res => {
      if (res.flag) {
        console.log(res);
        wx.showModal({
          title: '提示',
          content: '撤回审批成功',
          showCancel: false,
          complete: (res) => {
            if (res.confirm) {
              wx.navigateBack()
              wx.removeStorageSync('examInfo')
            }
          }
        })
        wx.hideLoading()
      }
    })
  },

  // 已阅
  readData() {
    var examMemberNum = '',
      examMemberType = "",
      listProps = this.data.listProps,
      that = this,
      url = app.globalData.url + 'exam/reviewExam';
    for (let i in listProps.btmList) {
      if (listProps.btmList[i].member_id == wx.getStorageSync('member_id')) {
        examMemberNum = listProps.btmList[i].examMember_num;
        examMemberType = listProps.btmList[i].examMember_type;
      }
    }
    wx.showLoading({
      title: '',
      icon: 'none'
    })
    app.wxRequest(url, {
      examMemberId: that.data.examInfo.examMember_id,
      examMemberNum: examMemberNum,
      examMemberDetail: that.data.examInfo.examMemberDetail == undefined ? '' : that.data.examInfo.examMemberDetail,
      examMemberStatus: 1,
      examType: 5,
      examMainId: that.data.examInfo.examMain_id,
      examMemberType: examMemberType
    }, res => {
      if (res.flag) {
        wx.hideLoading()
        wx.showModal({
          title: '提示',
          content: '审核成功',
          showCancel: false,
          complete: (res) => {
            if (res.confirm) {
              wx.navigateBack()
            }
          }
        })
      }
    })
  },

  // 获取签名版
  getPenSigner() {
    wx.showLoading({
      title: '',
      icon: 'none'
    })
    console.log(this.data.examInfo);
    var url = app.globalData.url + 'member/getPenSigner',
      that = this;
    app.wxRequest(url, {
      memberId: app.globalData.memberId
    }, res => {
      if (res.flag) {
        if (res.data.penSigners.length == 0) {
          that.setData({
            imgApproval: ''
          })
        } else {
          that.setData({
            imgApproval: res.data.penSigners[0].penSignerImg
          })
        }
      }
    })
  },


  //判断时间遮罩标题
  timeData(e) {
    this.setData({
      showTime: true,
      currentDate: new Date().getTime()
    })
  },
  // 选择时间
  resTime(e) {
    var time = new Date(e.detail),
      str = 'examMain.date_time',
      formartTime = this.formatDate(time);
    this.setData({
      [str]: formartTime,
      showTime: false
    })
  },
  formatDate(date) {
    return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
  },
  // 取消选择时间 
  selectCancel() {
    this.setData({
      showTime: false
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (wx.getStorageSync('examInfo') != '') {
      this.resultData()
    } else {
      this.getExamProcessDetail();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})